package net.easyjava.test.aws;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.*;

/**
 * Created by wxnacy on 16/11/24.
 */
public class SNSDemo {
    private static AmazonSNSClient snsClient;
    public static final String TOPIC_EASYJAVA_TEST = "easyjava-test";
    //arn:aws:sns:us-east-1:053227996260:easyjava-test
    public static final String ARN1 = "arn:aws:sns:us-east-1:053227996260:topic_1479964477571";
    public static final String ARN2 = "arn:aws:sns:us-east-1:053227996260:topic_1479964581059";

    public SNSDemo() {
        init();
    }

    public static void main(String[] args) {
        init();
        //创建主题
        String arn = createTopic(TOPIC_EASYJAVA_TEST);
        //订阅主题
//        subscribeTopic(ARN1);
        //想主题发送消息
//        String messageId = sendMessageToTopic(ARN1);
        //删除主题
//        deleteTopic(ARN2);


    }


    private static void init() {
        try {
            snsClient = new AmazonSNSClient(new SQSJMSDemo.AWSSQSCredentialsProvider());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建主题
     */
    private static String createTopic(String topicName) {
        //create a new SNS client and set endpoint
        snsClient.setRegion(Region.getRegion(Regions.US_EAST_1));
        //create a new SNS topic
        CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
        CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
        //print TopicArn
        System.out.println(createTopicResult);

        //get request id for CreateTopicRequest from SNS metadata
        System.out.println("CreateTopicRequest - " + snsClient.getCachedResponseMetadata(createTopicRequest));
        return createTopicResult.getTopicArn();
    }


    /**
     * 订阅主题
     * @param topicArn
     */
    private static void subscribeTopic(String topicArn){
        //subscribe to an SNS topic
        SubscribeRequest subRequest = new SubscribeRequest(topicArn, "email", "wenxiaoning@gochinatv.com");
        snsClient.subscribe(subRequest);
        //get request id for SubscribeRequest from SNS metadata
        System.out.println("SubscribeRequest - " + snsClient.getCachedResponseMetadata(subRequest));
        System.out.println("Check your email and confirm subscription.");
    }

    /**
     * 想主题发送消息
     * @param topicArn
     * @return
     */
    private static String sendMessageToTopic(String topicArn){
        //publish to an SNS topic
        String msg = "My first text published to SNS topic with email endpoint";
        PublishRequest publishRequest = new PublishRequest(topicArn, msg);
        PublishResult publishResult = snsClient.publish(publishRequest);
        //print MessageId of message published to SNS topic
        System.out.println("MessageId - " + publishResult.getMessageId());
        return publishResult.getMessageId();
    }

    public String sendMessageToTopic(String topicArn,String message){
        //publish to an SNS topic
        PublishRequest publishRequest = new PublishRequest(topicArn, message);
        PublishResult publishResult = snsClient.publish(publishRequest);
        //print MessageId of message published to SNS topic
        return publishResult.getMessageId();
    }


    /**
     * 删除主题
     * @param topicArn
     */
    private static void deleteTopic(String topicArn){
        //delete an SNS topic
        DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn);
        DeleteTopicResult result =  snsClient.deleteTopic(deleteTopicRequest);
        System.out.println("result.getSdkResponseMetadata() = " + result.getSdkResponseMetadata());
//get request id for DeleteTopicRequest from SNS metadata
        System.out.println("DeleteTopicRequest - " + snsClient.getCachedResponseMetadata(deleteTopicRequest));
    }
}
